# * REQUEST
#
# ```text
#        POST /runner/{shell-name}/{object}/{func}
# {
#    "id": "",     #  本次调用的唯一请求id
#    "object": "", #  被调用的对象的名字
#    "func": "",   #  被调用的对象的函数名称
#    "args": []    #  调用参数
# }
#
# ```
#
# * REPONSE
#
# ```text
# {
#    "id": "",     #  本次调用的唯一请求id
#    "object": "", #  被调用的对象的名字
#    "func": "",   #  被调用的对象的函数名称
#    "result": {}  #  返回的对象
#    "error": {
#        "id": 123,     # 错误码
#                "message": ""  # 错误描述
#    },
# }
# ```

class Error:
    def __init__(self, id: int = 0, message: str = ''):
        self.id = id  # 错误码
        self.message = str  # 错误描述

        @classmethod
        def load(cls, data: {}) -> object:
            error = Error()
            if 'id' in data:
                error.id = data['id']
            if 'message' in data:
                error.message = data['message']
            return error

        @classmethod
        def save(cls, obj) -> {}:
            if isinstance(obj, Error):
                return {
                    'id': obj.id,
                    'message': obj.message
                }
            return {}


class Request:
    def __init__(self):
        self.id = ''  # 本次调用的唯一请求id
        self.object = ''  # 被调用的对象的名字
        self.func = ''  # 被调用的对象的函数名称
        self.args = []  # 调用参数

    @classmethod
    def load(cls, data: {}) -> object:
        req = Request()
        if 'id' in data:
            req.id = data["id"]
        if 'object' in data:
            req.object = data["object"]
        if 'func' in data:
            req.func = data["func"]
        if 'args' in data:
            req.args = data["args"]

        return req

    @classmethod
    def save(cls, obj) -> {}:
        if isinstance(obj, Request):
            return {
                'id': obj.id,
                'object': obj.object,
                'func': obj.func,
                'args': obj.args,
            }

        return None


class Reponse:
    def __init__(self, req: Request = None, error: Error = None):
        if req is None:
            self.id = ''  # 本次调用的唯一请求id
            self.object = ''  # 被调用的对象的名字
            self.func = ''  # 被调用的对象的函数名称
            self.result = {}  # 返回的对象
            self.error = Error()
            return
        self.id = req.id  # 本次调用的唯一请求id
        self.object = req.object  # 被调用的对象的名字
        self.func = req.func  # 被调用的对象的函数名称
        self.result = {}  # 返回的对象
        if error is None:
            self.error = Error()
        else:
            self.error = error

    @classmethod
    def load(cls, data: {}) -> object:
        rep = Reponse()
        if 'id' in data:
            rep.id = data["id"]
        if 'object' in data:
            rep.object = data["object"]
        if 'func' in data:
            rep.func = data["func"]
        if 'result' in data:
            rep.result = data["result"]

        return rep

    @classmethod
    def save(cls, obj) -> {}:
        if isinstance(obj, Request):
            return {
                'id': obj.id,
                'object': obj.object,
                'func': obj.func,
                'args': obj.args,
            }
        return None
